package com.modules.system.controller;

import com.common.base.BaseController;
import com.modules.system.dto.SysMenuDto;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.common.base.Return;
import com.modules.system.entity.SysMenuEntity;
import com.modules.system.service.SysMenuService;
import java.util.List;

/**
 * 系统菜单
 * 
 */
@RestController
@RequestMapping("/sys/menu")
public class MenuController extends BaseController {

	@Autowired
	private SysMenuService sysMenuService;

	/**
	 * 导航菜单
	 */
	@RequestMapping(value = "/nav" , method = RequestMethod.GET)
	public Return nav(){
		List<SysMenuDto> menuList = sysMenuService.getUserMenuList(getUserId());
		return Return.success(menuList);
	}

	/**
	 * 所有菜单列表
	 */
	@RequestMapping(value = "/tree", method = RequestMethod.GET)
	public Return tree(){
		List<SysMenuDto> menuList = sysMenuService.getAllTree();
		return Return.success(menuList);
	}
	

	/**
	 * 菜单信息
	 */
	@RequestMapping(value = "/info/{menuId}", method = RequestMethod.GET)
	@RequiresPermissions("sys:menu:info")
	public Return info(@PathVariable("menuId") Long menuId){
		SysMenuEntity menu = sysMenuService.getByPrimaryKey(menuId);
		return Return.success(menu);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@RequiresPermissions("sys:menu:save")
	public Return save(@RequestBody SysMenuEntity menu){

		sysMenuService.insert(menu);
		
		return Return.success();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@RequiresPermissions("sys:menu:update")
	public Return update(@RequestBody SysMenuEntity menu){

		sysMenuService.updateByPrimaryKeySelective(menu);
		
		return Return.success();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
	@RequiresPermissions("sys:menu:delete")
	public Return delete(long menuId){

		sysMenuService.deleteWithAssociation(menuId);
		return Return.success();
	}

}
